<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>订单管理</title>
	<meta name="decorator" content="default"/>
</head>
<body>
	<ul class="nav nav-tabs">
	   <li class="active">
			<a href="${ctx}/bu/buExternalOrder/">外部订单列表</a></li>
			<shiro:hasPermission name="bu:buExternalOrder:add">
		<li><a href="${ctx}/bu/buExternalOrder/form?flag=1&write=0">外部订单添加</a></li>
			</shiro:hasPermission>
	</ul>
	<form id="searchForm" modelAttribute="buOrder" action="${ctx}/bu/buExternalOrder" method="post" class="breadcrumb form-search">
		<input type="hidden" id="pageNo" name="pageNo" value="${page.pageNo}"/>
		<input  type="hidden" id="pageSize" name="pageSize" value="${page.pageSize}"/>
		<input type="hidden" id="ors" name="ors" value="${orderState}"/>
		<input type="hidden" id="supVal" name="supVal" value="${supVal}"/>
		<input type="hidden" id="cusVal" name="cusVal" value="${cusVal}"/>
		<input type="hidden" id="salVal" name="salVal" value="${salVal}"/>
		<ul class="ul-form">
			<li><label>生产编号：</label>
				<input id="productNo" name="productNo" path="productNo" htmlEscape="false" maxlength="64" class="input-medium"/>
			</li>
			<li><label>下单日期：</label>
				<input id="beginOrderDate" name="beginOrderDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${buOrder.beginOrderDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/> - 
				<input id="endOrderDate" name="endOrderDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${buOrder.endOrderDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>	
			</li>
			<li><label>出货日期：</label>
				<input id="beginShipDate" name="beginShipDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${buOrder.beginShipDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/> - 
				<input id="endShipDate" name="endShipDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${buOrder.endShipDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
			</li>
			<li><label>产品名称：</label>
				<input id="productName" name="productName" path="productName" htmlEscape="false" maxlength="64" class="input-medium"/>
			</li>
			<li>
				<label>订单状态：</label>
				<select id="orderState" name="orderState" style="width: 180px;">
				       <shiro:hasPermission name="bu:buExternalOrder:operate:cusSelOrderState">
							<option value="-1">--请选择--</option>
							<option value="1000">待审核</option>
							<option value="1001">待生产</option>
							<option value="1002">生产中</option>
							<option value="1003">待发货</option>
							<option value="1004">待收货</option>
							<option value="1005">待收款</option>
							<option value="1006">已完成</option>
						</shiro:hasPermission>
						<shiro:hasPermission name="bu:buExternalOrder:operate:supSelOrderState">
							<option value="-1">--请选择--</option>
							<option value="1001">待生产</option>
							<option value="1002">生产中</option>
							<option value="1003">待发货</option>
							<option value="1004">待收货</option>
							<option value="1005">待收款</option>
							<option value="1006">已完成</option>
						</shiro:hasPermission>
					</select>
			</li>
			<shiro:hasPermission name="bu:buExternalOrder:operate:customer">
			    <li><label>业务员：</label>
			    	<select id="customerId" name="customerId" path="customerId" class="required input-xlarge " onchange="changSalesMan(this.options[this.options.selectedIndex].value)">
			    	    <option value="-1" label="--全部--"/>
			    	    <c:if test="${!empty customerId}">
			    	    	<options items="${customerList}" itemLabel="name" itemValue="id" htmlEscape="false" class="required"/>
			    	    </c:if>
						<options items="${customerList}" itemLabel="name" itemValue="id" htmlEscape="false" class="required"/>
					</select>
			    </li>
		    </shiro:hasPermission>
		    <shiro:hasPermission name="bu:buExternalOrder:operate:sales">
			    <li><label>客户：</label>
			    	<select id="salesmanId" name="salesmanId" path="salesmanId" class="required input-xlarge ">
			    		<option value="-1" label="--全部--"/>
						<options itemLabel="salesmanName" itemValue="id" htmlEscape="false"/>
					</select>
			    </li>
		    </shiro:hasPermission>
		    <shiro:hasPermission name="bu:buExternalOrder:operate:supplier">
			    <li><label>供应商：</label>
			    	<select id="supplierId" name="supplierId" path="supplierId" class="required input-xlarge ">
			    	    <option value="-1" label="--全部--"/>
						<options items="${supplierList}" itemLabel="name" itemValue="id" htmlEscape="false" class="required"/>
					</select>
			    </li>
		    </shiro:hasPermission>
			
			<li class="btns">
			     <input id="btnSubmit" class="btn btn-primary" type="button" value="查询" onclick="search()"/>&nbsp;&nbsp;<input id="btnReset" class="btn btn-primary" type="reset" value="重置"/>&nbsp;&nbsp;
				 <shiro:hasPermission name="bu:buExternalOrder:operate:btnExport">
					<input id="btnExport" class="btn btn-primary" type="button" value="导出"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				 </shiro:hasPermission>
				 <br/>
			</li>
			</br>
			<li>
			   <shiro:hasPermission name="bu:buExternalOrder:operate:salesTotal">
					销售总价之和:<span type="text" id="salesTotal" name="salesTotal" value="${entity.salesTotal}" style="width: 25px;"></span>&nbsp;&nbsp;&nbsp;&nbsp;
			   </shiro:hasPermission>
			   <shiro:hasPermission name="bu:buExternalOrder:operate:produceTotal">
					工厂总价之和: <span type="text" id="produceTotal" name="produceTotal" style="width: 45px;" value="${entity.produceTotal}"></span>&nbsp;&nbsp;&nbsp;&nbsp;
				</shiro:hasPermission>
				<shiro:hasPermission name="bu:buExternalOrder:operate:profitTotal">
					利润之和:<span id="profitTotal" name="profitTotal" value="${entity.profitTotal}" style="width: 45px;"></span>
				</shiro:hasPermission>
			</li>
		</ul>
	</form>
	<sys:message content="${message}"/>	
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr>
			    <th><input type=checkbox name="selid" id="checkId" onclick="checkAll(this, 'repairId')"></th>
				<th>图片</th>
				<th>生产编号</th>
				<th>下单日期</th>
				<th>出货日期</th>
				<th>产品名称</th>
				<th>工艺</th>
				<th>是否初订</th>
				<th>尺寸</th>
				<th>厚度</th>
				<th>数量</th>
				<th>状态</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody id="tbody">
			
		</tbody>
	</table>
	
	<div id="pageDiv" class="pagination">${page}</div>
	
	<script type="text/javascript">
		function addDelivery(href){
				top.$.jBox.open('iframe:'+href,'填写快递信息',$(top.document).width()-220,$(top.document).height()-180,{
					buttons:{"关闭":true},
					loaded:function(h){
						$(".jbox-content", top.document).css("overflow-y","hidden");
						$(".nav,.form-actions,[class=btn]", h.find("iframe").contents()).hide();
						$("body", h.find("iframe").contents()).css("margin","10px");
					}
				});
				return false;
			}
	</script>
	
	<script type="text/javascript">
	
		function ajaxUpdateDelivery(id,oldOrderState){
			
			var message = confirm("确定要操作该订单数据吗？");
			if(!message){
				return false;
			}else{
				
				$.ajax({
					type: 'GET',
					aync: true,
					url: '${ctx}/bu/buExternalOrder/ajaxCheckOrderState?id='+id+"&oldOrderState="+oldOrderState,
					success: function(result){
						 var json = eval("("+result+")");
						var code = json.code;
						if(code == 1){
							$.ajax({
								type: 'GET',
								aync: true,
								url: '${ctx}/bu/buExternalOrder/ajaxUpdateDelivery?id='+id+"&oldOrderState="+oldOrderState,
								success: function(result){
									console.log(result.code);
									search();
								}, error : function(json) {
						            $.jBox.alert("网络异常！");
						        }
							  });	
						}else{
							search();
						}
					}
				})
			}		
		
		}
		
		function ajaxExportProductFile(id){
			 var ors = $("#ors").val();
			 var supVal = $("#supVal").val();
			 var cusVal = $("#cusVal").val();
			 var salVal = $("#salVal").val();
			 var productNo = $("#productNo").val();
			 var beginOrderDate = $("#beginOrderDate").val();
			 var endOrderDate = $("#endOrderDate").val();
			 var shipDate = $("#shipDate").val();
			 var productName = $("#productName").val();
			 var orderState = $("#orderState").val();
			 var customerId = $("#customerId").val();
			 var salesmanId = $("#salesmanId").val();
			 var supplierId = $("#supplierId").val();
			 
			 var pageNo= $("#pageNo").val();
			 var pageSize=  $("#pageSize").val();
			
			$.ajax({
				type: 'POST',
				aync: true,
				dataType: 'jsonp',
				url: '${ctx}/bu/buExternalOrder/ajaxExportProductFile',
				data: {
					id: id,
					ors: ors,
					supVal: supVal,
					cusVal: cusVal,
					salVal: salVal,
					productNo: productNo,
					beginOrderDate: beginOrderDate,
					endOrderDate: endOrderDate,
					shipDate: shipDate,
					productName: productName,
					orderState: orderState,
					customerId: customerId,
					salesmanId: salesmanId,
					supplierId: supplierId
				},
				beforeSend:function(xhr){
				      console.log(xhr)
				      console.log('发送前')
				    },
				success:function(data,textStatus,jqXHR){
				      console.log(data)
				      console.log(textStatus)
				      console.log(jqXHR)
				    },
				error:function(xhr,textStatus){
				      console.log('错误')
				      console.log(xhr)
				      console.log(textStatus)
				    },
				complete:function(){
				      console.log('结束')
				    }
			});
		}
		
		function ajaxBatchReceipt(ids){
			 var ors = $("#ors").val();
			 var supVal = $("#supVal").val();
			 var cusVal = $("#cusVal").val();
			 var salVal = $("#salVal").val();
			 var productNo = $("#productNo").val();
			 var beginOrderDate = $("#beginOrderDate").val();
			 var endOrderDate = $("#endOrderDate").val();
			 var shipDate = $("#shipDate").val();
			 var productName = $("#productName").val();
			 var orderState = $("#orderState").val();
			 var customerId = $("#customerId").val();
			 var salesmanId = $("#salesmanId").val();
			 var supplierId = $("#supplierId").val();
			 
			 var pageNo= $("#pageNo").val();
			 var pageSize=  $("#pageSize").val();
			
			$.ajax({
				type: 'POST',
				aync: true,
				url: '${ctx}/bu/buExternalOrder/ajaxBatchReceipt',
				data: {
					ids: ids,
					ors: ors,
					supVal: supVal,
					cusVal: cusVal,
					salVal: salVal,
					productNo: productNo,
					beginOrderDate: beginOrderDate,
					endOrderDate: endOrderDate,
					shipDate: shipDate,
					productName: productName,
					orderState: orderState,
					customerId: customerId,
					salesmanId: salesmanId,
					supplierId: supplierId
				},
				dataType : "json",
				success: function(result){
					console.log(result.code);
					search();
				}, error : function(json) {
		            $.jBox.alert("网络异常！");
		        }
			});
		}
		
		function ajaxDownloadProductFile(){
			 var ors = $("#ors").val();
			 var supVal = $("#supVal").val();
			 var cusVal = $("#cusVal").val();
			 var salVal = $("#salVal").val();
			 var productNo = $("#productNo").val();
			 var beginOrderDate = $("#beginOrderDate").val();
			 var endOrderDate = $("#endOrderDate").val();
			 var shipDate = $("#shipDate").val();
			 var productName = $("#productName").val();
			 var orderState = $("#orderState").val();
			 var customerId = $("#customerId").val();
			 var salesmanId = $("#salesmanId").val();
			 var supplierId = $("#supplierId").val();
			 
			 var pageNo= $("#pageNo").val();
			 var pageSize=  $("#pageSize").val();
			
			$.ajax({
				type: 'POST',
				aync: true,
				url: '${ctx}/bu/buExternalOrder/ajaxDownloadProductFile',
				data: {
					ors: ors,
					supVal: supVal,
					cusVal: cusVal,
					salVal: salVal,
					productNo: productNo,
					beginOrderDate: beginOrderDate,
					endOrderDate: endOrderDate,
					shipDate: shipDate,
					productName: productName,
					orderState: orderState,
					customerId: customerId,
					salesmanId: salesmanId,
					supplierId: supplierId
				},
				beforeSend: function(xhr){
					console.log(xhr);
					console.log('-----------发送前-------------');
				},
				success: function(data,textStatus,jqXHR){
					console.log(data);
					console.log(textStatus);
					console.log(jqXHR);
					search();
				}, error : function(xhr,textStatus) {
					console.log(xhr);
					console.log(textStatus);
					console.log('-----------错误-------------');
		            $.jBox.alert("网络异常！");
		        },complete: function(){
		        	console.log('-----------结束-------------');
		        }
			});
		}
		
		function ajaxDelete(id){
			 var ors = $("#ors").val();
			 var supVal = $("#supVal").val();
			 var cusVal = $("#cusVal").val();
			 var salVal = $("#salVal").val();
			 var productNo = $("#productNo").val();
			 var beginOrderDate = $("#beginOrderDate").val();
			 var endOrderDate = $("#endOrderDate").val();
			 var shipDate = $("#shipDate").val();
			 var productName = $("#productName").val();
			 var orderState = $("#orderState").val();
			 var customerId = $("#customerId").val();
			 var salesmanId = $("#salesmanId").val();
			 var supplierId = $("#supplierId").val();
			 
			 var pageNo= $("#pageNo").val();
			 var pageSize=  $("#pageSize").val();
			
			$.ajax({
				type: 'POST',
				aync: true,
				url: '${ctx}/bu/buExternalOrder/ajaxDelete',
				data: {
					id: id,
					ors: ors,
					supVal: supVal,
					cusVal: cusVal,
					salVal: salVal,
					productNo: productNo,
					beginOrderDate: beginOrderDate,
					endOrderDate: endOrderDate,
					shipDate: shipDate,
					productName: productName,
					orderState: orderState,
					customerId: customerId,
					salesmanId: salesmanId,
					supplierId: supplierId
				},
				dataType : "json",
				success: function(result){
					console.log(result.code);
					search();
				}, error : function(json) {
		            $.jBox.alert("网络异常！");
		        }
			});
		}
	
		function search(){
			 var ors = $("#ors").val();
			 var supVal = $("#supVal").val();
			 var cusVal = $("#cusVal").val();
			 var salVal = $("#salVal").val();
			 var productNo = $("#productNo").val();
			 
			 var beginOrderDate = $("#beginOrderDate").val();
			 var endOrderDate = $("#endOrderDate").val();
			 var beginShipDate = $("#beginShipDate").val();
			 var endShipDate = $("#endShipDate").val();
			 var shipDate = $("#shipDate").val();
			 
			 var productName = $("#productName").val();
			 var orderState = $("#orderState").val();
			 var customerId = $("#customerId").val();
			 var salesmanId = $("#salesmanId").val();
			 var supplierId = $("#supplierId").val();
			 
			 var pageNo= $("#pageNo").val();
			 var pageSize=  $("#pageSize").val();
			 console.log("search pageNo       ",pageNo);
			 console.log("search pageSize     ",pageSize);
			
			$.ajax({
				type : "post",
				async : true,    
				url : "${ctx}/bu/buExternalOrder/json",
				data: {
					ors: ors,
					supVal: supVal,
					cusVal: cusVal,
					salVal: salVal,
					productNo: productNo,
					
					beginOrderDate: beginOrderDate,
					endOrderDate: endOrderDate,
					beginShipDate: beginShipDate,
					endShipDate: endShipDate,
					shipDate: shipDate,
					
					productName: productName,
					orderState: orderState,
					customerId: customerId,
					salesmanId: salesmanId,
					supplierId: supplierId,
					pageNo: pageNo,
					pageSize: pageSize
				},
				dataType : "json",
				success: function(result){
					if(result.resultCode == 222){
						$("#contentTable tbody").empty();
						$("#pageDiv").empty();
						$("#pageNo").empty();
						$("#pageSize").empty();
						document.getElementById("checkId").checked=false;
						var resultPage = result.page;
						var picPrefix = result.picPrefix;
						var lists = resultPage.list;
						var entity = result.entity;
						var supplierList = result.supplierList;
						var customerList = result.customerList;
						supplierOptions(supplierList);
						customerOptions(customerList);
						var orderState = result.orderState;
						console.log("orderState       ",orderState);
						console.log("orderState       ",typeof orderStatfe);
						if(orderState == 1005){
							$("#ckr").remove();
							var betchInfo = "<shiro:hasPermission name='bu:buExternalOrder:operate:batchReceipt'>"
								          	+ "<a href='#' id='ckr' name='ckr' onclick='checkRec()' class='btn btn-primary'>批量收款</a>"
					 					  + "</shiro:hasPermission>";
							$("#btnSubmit").after(betchInfo);
						}else{
							if($("#ckr").length > 0){
								$("#ckr").remove();
							}
						}
						if(entity != "" && entity != null){
							$("#profitTotal").html(entity.profitTotal);
							$("#produceTotal").html(entity.produceTotal);
							$("#salesTotal").html(entity.salesTotal);
						}else{
							$("#profitTotal").html("");
							$("#produceTotal").html("");
							$("#salesTotal").html("");
						}
							var supSel = $("#supplierId").select2();
							supSel.val(result.supVal).trigger("change");
							supSel.change();
							
							var cusSel = $("#customerId").select2();
							cusSel.val(result.cusVal).trigger("change");
							cusSel.change();
							
							var salSel = $("#salesmanId").select2();
							var salVal = $("#salVal").val(result.salVal);
							salSel.val(result.salVal).trigger('change');
							salSel.change();
							
							var customerId = $("#customerId").find("option:selected").val();
							changSalesMan(customerId);
							
							var ordSel = $("#orderState").select2();    //获取select 的ID
					        ordSel.val(result.orderState).trigger("change");    //这个values就是代表你选中的值
					        ordSel.change();
						
						if(resultPage.list == null){
							 	var pageDiv="<div  class='pagination'>"+result.page.html+"</div>";
				                var html="<tr>"+
				                    "<td colspan='12' style='width: 100%;height: 40px;text-align: center;'>"+"没有相关数据"+"</td>";
				                    +" </tr>";
				                $("#contentTable").append(html);
				                $("#pageNo").append("<input type='hidden' id='pageNo' name='pageNo' value='"+page.pageNo+"'/>");
				                $("#pageSize").append("<input type='hidden' id='pageSize' name='pageSize' value='"+page.pageSize+"'/>");
				                $("#ors").append("<input type='hidden' id='ors' name='ors' value='"+ors+"'/>");
				                $("#supVal").append("<input type='hidden' id='supVal' name='supVal' value='"+supVal+"'/>");
				                $("#cusVal").append("<input type='hidden' id='cusVal' name='cusVal' value='"+cusVal+"'/>");
				                $("#salVal").append("<input type='hidden' id='salVal' name='salVal' value='"+salVal+"'/>");
				                $("#pageDiv").append(pageDiv);
						}else{
							var pageDiv = "<div  class='pagination'>"+result.page.html+"</div>";
							for(i in lists){
								var status = parseInt(lists[i].status);
								var orderState = parseInt(lists[i].orderState);
								var isStatus = "";
								var orderStateStr = "";
								if(status == 0){
									isStatus = "初订";
								}else{
									isStatus = "续订";
								}
								var eos0Role = "";
								var toUpdateRole = "";
								var eos1Role = "";
								var exportRole = "";
								var suExportRole = "";
								var eos2Role = "";
								var eos3Role = "";
								var eos4Role = "";
								var eos5Role = "";
								var copyRole = "";
								var gendanCopyRole = "";
								
								if(orderState == 1002){
									suExportRole = "<a class='operate' href='${ctx}/bu/buExternalOrder/exportProductFile?id="+lists[i].id+"' onclick='return confirmx('确认要下载数据吗？', this.href)'>下载生产单</a>";
								}
								if(orderState == 1000){
									toUpdateRole =  "<a class='operate' href='${ctx}/bu/buExternalOrder/toUpdate?id="+lists[i].id+"&flag=1&write=0'>修改</a>";
									eos0Role = "<a class='operate' href='javascript:void(0);' onclick=\"return ajaxUpdateDelivery('"+lists[i].id+"','"+lists[i].orderState+"')\">审核</a>";
									orderStateStr = "待审核";
								}else if(orderState == 1001){
									eos1Role = "<a class='operate' href='javascript:void(0);' onclick=\"return ajaxUpdateDelivery('"+lists[i].id+"','"+lists[i].orderState+"')\">确认生产</a>";
								    exportRole = "<a href='${ctx}/bu/buExternalOrder/exportProductFile?id="+lists[i].id+"' onclick='return confirmx('确认要下载数据吗？', this.href)'>下载生产单</a>";
									orderStateStr = "待生产";
								}else if(orderState == 1002){
									eos2Role = "<a class='operate' href='javascript:void(0);' onclick=\"return ajaxUpdateDelivery('"+lists[i].id+"','"+lists[i].orderState+"')\">生产完成</a>";
									orderStateStr = "生产中";
								}else if(orderState == 1003){
									 eos3Role = "<a class='operate' href='${ctx}/bu/buExternalOrder/selectById?id="+lists[i].id+"&flag=1&write=1' onclick='return confirmx('确认操作该条订单数据吗？', this.href)'>发货</a>";
									orderStateStr = "待发货";
								}else if(orderState == 1004){
									eos4Role = "<a class='operate' href='javascript:void(0);' onclick=\"return ajaxUpdateDelivery('"+lists[i].id+"','"+lists[i].orderState+"')\">确认收货</a>";
									orderStateStr = "待收货";
								}else if(orderState == 1005){
									gendanCopyRole = "<a class='operate' href='${ctx}/bu/buExternalOrder/gendanCopy?id="+lists[i].id+"&flag=3&write=0'>追加</a>";
								 	eos5Role = "<a class='operate' href='javascript:void(0);' onclick=\"return ajaxUpdateDelivery('"+lists[i].id+"','"+lists[i].orderState+"')\">确认收款</a>";
									orderStateStr = "待收款";
								}else if(orderState == 1006){
									copyRole = "<a class='operate' href='${ctx}/bu/buExternalOrder/copy?id="+lists[i].id+"&flag=3&write=0'>追加</a>";
									orderStateStr = "已完成";
								}
								mamgerToUpdate = "<a class='operate' href='${ctx}/bu/buExternalOrder/manageToUpdate?id="+lists[i].id+"&flag=1&write=0'>修改</a>";
								toDelete = "<a class='operate' href='javascript:void(0);' onclick=\"ajaxDelete('"+lists[i].id+"')\">删除</a>";
							var html = "<tr><td class='imagecenter'><input type='checkbox' name='repairId' value='"+lists[i].id+"' onclick='selectSingle()'/></td>"
								     + "<td class='textcenter'><img alt='' src='"+picPrefix+lists[i].picUrl+"' style='width: 90px;height: 90px;'></td>"
								     + "<td class='textcenter'><a href='${ctx}/bu/buExternalOrder/view?id="+lists[i].id+"'>"+lists[i].productNo+"</a></td>"
								     + "<td class='textcenter'>"+lists[i].orderDate+"</td>"
									 + "<td class='textcenter'>"+lists[i].shipDate+"</td>"
									 + "<td class='textcenter'>"+lists[i].productName+"</td>"
									 + "<td class='textcenter'>"+lists[i].crafts+"</td>"
									 + "<td class='textcenter'>"+isStatus+"</td>"
									 + "<td class='textcenter'>"+lists[i].productSize+"</td>"
									 + "<td class='textcenter'>"+lists[i].thinkness+"</td>"
									 + "<td class='textcenter'>"+lists[i].quantity+"</td>"
									 + "<td class='textcenter'>"+orderStateStr+"</td>"
									 + "<td class='textcenter'><a href='${ctx}/bu/buExternalOrder/view?id="+lists[i].id+"&flag=2&write=0'>查看</a>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:eos0'>"
							 				+ eos0Role
							 			+ "</shiro:hasPermission>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:eos1'>"
						 					+ eos1Role
						    			+ "</shiro:hasPermission>"
						    			+ "<shiro:hasPermission name='bu:buExternalOrder:operate:supexport'>"
						    			    + suExportRole
						    			+  "</shiro:hasPermission>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:export'>"
											+ exportRole
										+ "</shiro:hasPermission>" 
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:eos2'>"
						 					+ eos2Role
						 				+ "</shiro:hasPermission>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:eos3'>"
								 			+ eos3Role
								 		+ "</shiro:hasPermission>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:eos4'>"
						 					+ eos4Role
						 				+ "</shiro:hasPermission>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:eos5'>"
											+ eos5Role
										+ "</shiro:hasPermission>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:copy'>"
						 			    	+ copyRole
						 				+ "</shiro:hasPermission>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:gendanOperate:gendanCopy'>"
					 				    	+ gendanCopyRole
					 					+ "</shiro:hasPermission>"
									 	+ "<shiro:hasPermission name='bu:buExternalOrder:operate:toUpdate'>"
					 						+ toUpdateRole
					 				    + "</shiro:hasPermission>"
									    + "<shiro:hasPermission name='bu:buExternalOrder:operate:manageToUpdate'>"
									 		+ mamgerToUpdate
									 	+ "</shiro:hasPermission>"
									    + "<shiro:hasPermission name='bu:buExternalOrder:operate:delete'>"
									 		+ toDelete
									    + "</shiro:hasPermission>"
									 + "</td>"
								     + "</tr>";
									 $("#contentTable").append(html);
							}
							$("#pageNo").append("<input type='hidden' id='pageNo' name='pageNo' value='"+page.pageNo+"'/>");
			                $("#pageSize").append("<input type='hidden' id='pageSize' name='pageSize' value='"+page.pageSize+"'/>");
			                $("#ors").append("<input type='hidden' id='ors' name='ors' value='"+ors+"'/>");
			                $("#supVal").append("<input type='hidden' id='supVal' name='supVal' value='"+supVal+"'/>");
			                $("#cusVal").append("<input type='hidden' id='cusVal' name='cusVal' value='"+cusVal+"'/>");
			                $("#salVal").append("<input type='hidden' id='salVal' name='salVal' value='"+salVal+"'/>");
			                
			                $("#pageDiv").append(pageDiv);
			                dealWithTable();
						}
					}
				}, error : function(json) {
					console.log("3333333333333333333333333333333");
		            $.jBox.alert("网络异常！");
		        }
			});
		}
		
		function supplierOptions(supplierList){
			$("#supplierId").empty();
	            if (supplierList.length > 0) {
	            	if(supplierList.length == 1){
		                for (var i = 0; i < supplierList.length; i++) {
		                        var partId = supplierList[i].id;
		                        var partName = supplierList[i].name;
		                        $("#supplierId").append("<option value='-1'>--全部--</option>");
		                        $("#supplierId").append("<option value="+partId+">"+partName+"</option>");
		                }
	            	}else{
	            		$("#supplierId").append("<option value='-1'>--全部--</option>");
	            		 for (var i = 0; i < supplierList.length; i++) {
		                        var partId = supplierList[i].id;
		                        var partName = supplierList[i].name;
		                        $("#supplierId").append("<option value="+partId+">"+partName+"</option>")
		                }
	            	}
	            	  var ddd = $("#supplierId").select2();    //获取select 的ID
	            	  ddd.val(supVal).trigger("change");    //这个values就是代表你选中的值
		  	          ddd.change();	
	        }else{
	        	$("#supplierId").empty();
	        	$("#supplierId").append("<option value='-1'>--全部--</option>");
	        }
		}
		
		function customerOptions(customerList){
			$("#customerId").empty();
	            if (customerList.length > 0) {
	            	if(customerList.length == 1){
		                for (var i = 0; i < customerList.length; i++) {
		                        var partId = customerList[i].id;
		                        var partName = customerList[i].name;
		                        $("#customerId").append("<option value='-1'>--全部--</option>");
		                        $("#customerId").append("<option value="+partId+">"+partName+"</option>");
		                }
	            	}else{
	            		$("#customerId").append("<option value='-1'>--全部--</option>");
	            		 for (var i = 0; i < customerList.length; i++) {
		                        var partId = customerList[i].id;
		                        var partName = customerList[i].name;
		                        $("#customerId").append("<option value="+partId+">"+partName+"</option>")
		                }
	            	}
	            	  var ddd = $("#customerId").select2();    //获取select 的ID
		  	          ddd.val(cusVal).trigger("change");    //这个values就是代表你选中的值
		  	          ddd.change();	
	        }else{
	        	$("#customerId").empty();
	        	$("#salesmanId").append("<option value='-1'>--全部--</option>");
	        }
		}
	
		function changSalesMan(v){
		   	 var salVal = $("#salVal").val();
		  	 var customerIdVal = $("#customerId").val();
		  	 if(customerIdVal != "" && customerIdVal != '-1'){
		  		$("#salesmanId").empty();
				$("#salesmanId").append("<option value='-1'>--全部--</option>");
			    $.ajax({
			        type : "post",
			        async : true,
			        url : "${ctx}/bu/buExternalOrder/selectSalesList",
			        data : {
			            'id' : v
			        },
			        dataType : "json",
			        success : function(msg) {
			            $("#salesmanId").empty();
				            if (msg.salesmanId.length > 0) {
				            	if(msg.salesmanId.length == 1){
                                                       $("#salesmanId").append("<option value='-1'>--全部--</option>");
					                for (var i = 0; i < msg.salesmanId.length; i++) {
					                        var partId = msg.salesmanId[i].id;
					                        var partName = msg.salesmanId[i].salesmanName;
					                        $("#salesmanId").append("<option value='-1'>--全部--</option>");
					                        $("#salesmanId").append("<option value="+partId+">"+partName+"</option>");
					                }
				            	}else{
				            		$("#salesmanId").append("<option value='-1'>--全部--</option>");
				            		 for (var i = 0; i < msg.salesmanId.length; i++) {
					                        var partId = msg.salesmanId[i].id;
					                        var partName = msg.salesmanId[i].salesmanName;
					                        $("#salesmanId").append("<option value="+partId+">"+partName+"</option>")
					                }
				            	}
				            	  var ddd = $("#salesmanId").select2();    //获取select 的ID
					  	          ddd.val(salVal).trigger("change");    //这个values就是代表你选中的值
					  	          ddd.change();	
				        }else{
				        	$("#salesmanId").empty();
				        	$("#salesmanId").append("<option value='-1'>--全部--</option>");
				        }
			    }, error : function(json) {
		            $.jBox.alert("网络异常！");
		        }
			    }); 
		  	 }else{
		  		$("#salesmanId").empty();
	        	$("#salesmanId").append("<option value='-1'>--全部--</option>");
		  	 }
			   
	   }
	
	    $(document).ready(function(){
	    	
	    	search();
	    	var customerId = $("#customerId").find("option:selected").val();
			changSalesMan(customerId);
			  
			var supSel = $("#supplierId").select2();
			var supVal = $("#supVal").val();
			supSel.val(supVal).trigger("change");
			supSel.change();
			
			var cusSel = $("#customerId").select2();
			var cusVal = $("#cusVal").val();
			cusSel.val(cusVal).trigger("change");
			cusSel.change();
			
			var ordSel = $("#orderState").select2();    //获取select 的ID
			var ordVal = $("#ors").val();
	        ordSel.val(ordVal).trigger("change");    //这个values就是代表你选中的值
	        ordSel.change();
			  
			$("#btnExport").click(function(){
				top.$.jBox.confirm("确认要导出订单数据吗？","系统提示",function(v,h,f){
					if(v=="ok"){
						$("#searchForm").attr("action","${ctx}/bu/buExternalOrder/downloadProductFile");
						$("#searchForm").submit();
						$("#searchForm").removeAttr("action","${ctx}/bu/buExternalOrder/downloadProductFile");
					}
				},{buttonsFocus:1});
				top.$('.jbox-body .jbox-icon').css('top','55px');
			});
			$("#btnReset").click(function(){
				$("#productNo").attr("value","");
				$("#beginOrderDate").attr("value","");
				$("#endOrderDate").attr("value","");
				$("#beginShipDate").attr("value","");
				$("#endShipDate").attr("value","");
				$("#shipDate").attr("value","");
				$("#productName").attr("value","");
				
				$("#salVal").val("");
				$("#supVal").val("");
				$("#cusVal").val("");
				
				$("#orderState").val("-1").trigger("change");
				$("#supplierId").val("-1").trigger("change");
				$('#customerId').val('-1').trigger('change');
				 
			});
			
		});
		function page(n,s){
			$("#pageNo").val(n);
			$("#pageSize").val(s);
			search();
        	return false;
        }
		// 全选按钮点击事件
		function checkAll(e, itemName){
		        var repairId = document.getElementsByName("repairId");    //获取全选复选框
		        for (var i=0; i<repairId.length; i++){
		         repairId[i].checked = e.checked;    //改变所有复选框的状态为全选复选框的状态
		       }
		    }

		/*根据单个复选框的选择情况确定全选复选框是否被选中*/
		function selectSingle(){
			var k=0;
			var oInput=document.getElementsByName("repairId");
			for (var i=0;i<oInput.length;i++){ 
				if(oInput[i].checked==false){
					k=1;
					break;
				}}if(k==0){
					 document.getElementById("checkId").checked=true;
				}else{
					document.getElementById("checkId").checked=false;
					}
				}
	
		function checkRec(){
			  var num = $("input[type='checkbox']:checked").length;
			  if(num == 0){
				  //confirmx("请选择你要批量收款的订单数据");
				  alert("请选择你要批量收款的订单数据");
				  return false;
			  }else{
			  	  confirmx('确定要批量收款的订单数据吗？',repairDel);
			  }}
		
		function repairDel(){
		      //批量
		      var id = document.getElementsByName("repairId");//获取全选复选框
		      var ids = "";//用于拼接所有已选中的id
		      //拼接所有id
		      for(var i=0;i<id.length;i++){
		    	  if(id[i].checked){
		    		  ids+=id[i].value+",";
		    		  }}
		          ids=ids.substring(0, ids.length-1);//干掉字符串最后一个逗号
		          ajaxBatchReceipt(ids);
		     }
	</script>
	
	<script>
	   //计算天数差的函数，通用  
	   function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2002-12-18格式  
	       var  aDate,  oDate1,  oDate2,  iDays  
	       aDate  =  sDate1.split("-")  
	       oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2002格式  
	       aDate  =  sDate2.split("-")  
	       oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
	       iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
	       return  iDays  
		}
		function getFormatYMD(timesamp){
			var date = new Date(timesamp);
			Y = date.getFullYear() + '-';
			M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
			D = date.getDate();
			D= D.toString().length==1 ? '0'+D:D;
			return Y+M+D;
		}
		
		function dealWithTable(){
			$("table tr").each(function(i) {
		    	var orderState = "";
		    	$("table tr").eq(i).find("td").eq(11).each(function() {
		    		var dataStr = $(this).text().replace(/(^\s*)|(\s*$)/g, "");
		    		console.log("dataStr    ",dataStr);
		    		orderState = dataStr;
		    	});
		        $("table tr").eq(i).find("td").eq(4).each(function() {
		            var dataStr = $(this).html();
		            var dateArr = dataStr.split('-');
		            var date1 = Date.parse(new Date(dateArr[0],dateArr[1]-1,dateArr[2]));
		            var now = new Date();
		            var date2 = Date.parse(new Date());
		            var ssArr = getFormatYMD(date1);
		            var eeArr = getFormatYMD(date2);
		            var sArr = ssArr.split("-");
		            var eArr = eeArr.split("-");
		            var sRDate = Date.parse(new Date(sArr[0], sArr[1], sArr[2]));
		            var eRDate = Date.parse(new Date(eArr[0], eArr[1], eArr[2]));
		            var days = (eRDate - sRDate)/(24*60*60*1000);  
		            var diff = parseInt(Math.abs(date1 - now) / 1000 / 60 / 60);
		            var gap = DateDiff(now,date1);
		            var Str = "生产中";
		            if(orderState == Str){
		            	if (days == -1) {
			            	console.log("33333333333333333333");
			            	$("table tr").eq(i).addClass("redtime2");
			            }else if(days == -2){
			            	console.log("22222222222222222222");
			            	$("table tr").eq(i).addClass("redtime3");
			            }else if(days == 0){
			            	console.log("11111111111111111111");
			            	$("table tr").eq(i).addClass("redtime1");
			            }else if(days >= 1){
			            	console.log("11111111111111111111");
			            	$("table tr").eq(i).addClass("redtime0");
			            }
		            }else{
		            	console.log("222             ",orderState);
		            }
		        })
		    });
		}
		function DateDiff(Date1, Date2) {
		    var iDays;
		    iDays = parseInt((Date1 - Date2) / 1000 / 60 / 60 / 24) + 1;
		    return iDays;
		}
	
	</script>

	<style>
	    .table-striped tbody>tr:nth-child(odd)>td, .table-striped tbody>tr:nth-child(odd)>th{background-color:rgba(255,255,255,0.2);}
	    .table-striped tbody>tr:nth-child(even)>td, .table-striped tbody>tr:nth-child(even)>th{background-color:rgba(255,255,255,0.2);}
	    .table-striped tbody>tr:nth-child(odd)>td, .table-striped tbody>tr:nth-child(odd)>th{text-align:center;}
	    .table-striped tbody>tr:nth-child(even)>td, .table-striped tbody>tr:nth-child(even)>th{text-align:center;}
		.redtime2 {background-color:#fdedeb;}
		.redtime3 {background-color:#FFF8DC;}
		.redtime1 {background-color:#ecb4d6;}
		.redtime0 {background-color:#dc7878;}
		.operate {display:inline-block;padding: 0 5px;}
		.textcenter {text-align:center;min-width:80px;}
		.imagecenter {text-align:center;width:25px;}
		.table-striped thead>tr>th{text-align:center;}
	</style>
	
</body>
</html>